package agenda;

import agenda.business.AgendaBO;
import agenda.model.MdAgenda;
import agenda.model.TableModel.AgendaTableModel;
import agenda.view.Agenda;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.TrayIcon;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class InterfaceAgenda extends JFrame {

    private JPanel contentPane;
    private JTable tabela_contatos;
    public static JTextField textField_nome;
    public static JTextField textField_sobrenome;
    public static JTextField textField_telefoneFixo;
    public static JTextField textField_telefoneMovel;
    public AgendaBO agendaBO = new AgendaBO();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InterfaceAgenda frame = new InterfaceAgenda();
                    frame.setVisible(true);
                    frame.lista();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private AgendaTableModel tableModel;

    public void lista() {
        tableModel = new AgendaTableModel(agendaBO.listarTodos());
        tabela_contatos.setEnabled(true);

        tabela_contatos.setModel(tableModel);
    }

    /**
     * Create the frame.
     */
    public InterfaceAgenda() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 705, 473);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();

        JPanel panel = new JPanel();

        JLabel lblNewLabel = new JLabel("Agenda de Contatos");
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setFont(new Font("Sylfaen", Font.BOLD | Font.ITALIC, 23));
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 679, Short.MAX_VALUE)
                .addGroup(gl_contentPane.createSequentialGroup()
                .addComponent(panel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(10))
                .addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
                .addContainerGap(237, Short.MAX_VALUE)
                .addComponent(lblNewLabel)
                .addGap(228)));
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_contentPane.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNewLabel)
                .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 265, GroupLayout.PREFERRED_SIZE)));

        JButton btnSalvar = new JButton("Salvar");
        btnSalvar.setFont(new Font("Tahoma", Font.BOLD, 12));
        btnSalvar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Agenda.addContato();

                MdAgenda agenda = new MdAgenda();
                Boolean erro = Boolean.FALSE;
                if (textField_nome.getText() == null || textField_nome.getText().trim().equals("")) {
                    JOptionPane.showMessageDialog(rootPane, "Preencha o campo 'Nome'", "Preencha os campos obrigatórios", JOptionPane.INFORMATION_MESSAGE);
                    erro = Boolean.TRUE;
                } else if (textField_sobrenome.getText() == null || textField_sobrenome.getText().trim().equals("")) {
                    JOptionPane.showMessageDialog(rootPane, "Preencha o campo 'Sobrenome'", "Preencha os campos obrigatórios", JOptionPane.INFORMATION_MESSAGE);
                    erro = Boolean.TRUE;
                } else if (textField_telefoneFixo.getText() == null || textField_telefoneFixo.getText().trim().equals("")) {
                    JOptionPane.showMessageDialog(rootPane, "Preencha o campo 'Telefone Fixo'", "Preencha os campos obrigatórios", JOptionPane.INFORMATION_MESSAGE);
                    erro = Boolean.TRUE;
                } else if (textField_telefoneMovel.getText() == null || textField_telefoneMovel.getText().trim().equals("")) {
                    JOptionPane.showMessageDialog(rootPane, "Preencha o campo 'Telefone Movel'", "Preencha os campos obrigatórios", JOptionPane.INFORMATION_MESSAGE);
                    erro = Boolean.TRUE;
                }

                if (!erro) {
                    agenda.setNome(textField_nome.getText());
                    agenda.setSobrenome(textField_sobrenome.getText());
                    agenda.setTelefoneFixo(textField_telefoneFixo.getText());
                    agenda.setTelefoneMovel(textField_telefoneMovel.getText());

                    agendaBO.incluir(agenda);

                    lista();
                }


            }
        });

        JButton btnLimpar = new JButton("Limpar");
        btnLimpar.setFont(new Font("Tahoma", Font.BOLD, 12));
        btnLimpar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField_nome.setText("");
                textField_sobrenome.setText("");
                textField_telefoneFixo.setText("");
                textField_telefoneMovel.setText("");
            }
        });


        JButton btnExcluir = new JButton("Excluir");
        btnExcluir.setFont(new Font("Tahoma", Font.BOLD, 12));
        btnExcluir.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(tabela_contatos.getSelectedRow()>=0){
                 agendaBO.excluir(tableModel.agendas.get(tabela_contatos.getSelectedRow()));
                }
                lista();
//				((DefaultTableModel) tabela_contatos.getModel()).removeRow(tabela_contatos.getSelectedRow());  
            }
        });

        JLabel lblNome = new JLabel("Nome:");
        lblNome.setFont(new Font("Tahoma", Font.BOLD, 12));

        textField_nome = new JTextField();
        textField_nome.setColumns(10);

        JLabel lblSobreno = new JLabel("Sobrenome:");
        lblSobreno.setFont(new Font("Tahoma", Font.BOLD, 12));

        textField_sobrenome = new JTextField();
        textField_sobrenome.setColumns(10);

        JLabel lblTelefoneFixo = new JLabel("Telefone Fixo: ");
        lblTelefoneFixo.setFont(new Font("Tahoma", Font.BOLD, 12));

        textField_telefoneFixo = new JTextField();
        textField_telefoneFixo.setColumns(10);

        JLabel lblTelefoneMvel = new JLabel("Telefone M\u00F3vel:");
        lblTelefoneMvel.setFont(new Font("Tahoma", Font.BOLD, 12));

        textField_telefoneMovel = new JTextField();
        textField_telefoneMovel.setColumns(10);
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                .addGap(222)
                .addComponent(btnLimpar)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnSalvar)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(btnExcluir))
                .addGroup(gl_panel.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
                .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
                .addComponent(lblTelefoneFixo)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(textField_telefoneFixo))
                .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
                .addComponent(lblNome)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(textField_nome, GroupLayout.PREFERRED_SIZE, 227, GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel.createSequentialGroup()
                .addComponent(lblSobreno)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(textField_sobrenome, GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
                .addGroup(gl_panel.createSequentialGroup()
                .addComponent(lblTelefoneMvel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(textField_telefoneMovel, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)))))
                .addContainerGap()));
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                .addComponent(lblNome)
                .addComponent(textField_nome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(lblSobreno)
                .addComponent(textField_sobrenome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                .addComponent(lblTelefoneFixo)
                .addComponent(textField_telefoneFixo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(lblTelefoneMvel)
                .addComponent(textField_telefoneMovel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                .addComponent(btnLimpar, Alignment.TRAILING)
                .addComponent(btnSalvar, Alignment.TRAILING)
                .addComponent(btnExcluir, Alignment.TRAILING))
                .addContainerGap()));
        panel.setLayout(gl_panel);

        tabela_contatos = new JTable();
        tabela_contatos.setFont(new Font("Tahoma", Font.PLAIN, 11));
        scrollPane.setViewportView(tabela_contatos);
        contentPane.setLayout(gl_contentPane);
    }
}
